//
//  UIViewController+CustomNavigation.m
//  TB
//
//  Created by cuibaoyin on 14-4-5.
//  Copyright (c) 2014年 TB. All rights reserved.
//

#import "UIViewController+NavigationBarSetting.h"

@implementation UIViewController (NavigationBarSetting)

- (void)navigationBarItem:(NavigationBarItem)item image:(UIImage *)image action:(SEL)action
{
    NSAssert(image != nil, @"image must not be nil!");
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat buttonWidth = image.size.width > 44.0 ? image.size.width : 44.0;
    leftButton.frame = CGRectMake(0, 0, buttonWidth, 44);
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    if (item == NavigationBarItemLeft)
    {
        self.navigationItem.leftBarButtonItem = barItem;
    }
    else if (item == NavigationBarItemRight)
    {
        self.navigationItem.rightBarButtonItem = barItem;
    }
}

- (void)navigationBarItem:(NavigationBarItem)item attributeTitle:(NSAttributedString *)attrbuteTitle action:(SEL)action
{
    NSAssert(attrbuteTitle != nil, @"attributeTitle must not be nil!");
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];

    //计算button的宽度，不小于44.0
    CGRect stringRect = [attrbuteTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 44.0) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading context:nil];
    CGFloat buttonWidth = stringRect.size.width > 44.0 ? stringRect.size.width : 44.0;
    leftButton.frame = CGRectMake(0, 0, buttonWidth, 44);

    [leftButton setAttributedTitle:attrbuteTitle forState:UIControlStateNormal];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    if (item == NavigationBarItemLeft)
    {
        self.navigationItem.leftBarButtonItem = barItem;
    }
    else if (item == NavigationBarItemRight)
    {
        self.navigationItem.rightBarButtonItem = barItem;
    }
}

- (void)navigationBarItem:(NavigationBarItem)item image:(UIImage *)image attributeTitle:(NSAttributedString *)attrbuteTitle action:(SEL)action
{
    NSAssert(image != nil && attrbuteTitle != nil, @"image and attributeTitle must not be nil!");

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat buttonWidth = image.size.width > 44.0 ? image.size.width : 44.0f;
    [leftButton setFrame:CGRectMake(0, 0, buttonWidth, 44.0f)];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setAttributedTitle:attrbuteTitle forState:UIControlStateNormal];
    [leftButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -buttonWidth * 0.5, 0, 0)];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    if (item == NavigationBarItemLeft)
    {
        self.navigationItem.leftBarButtonItem = barItem;
    }
    else if (item == NavigationBarItemRight)
    {
        self.navigationItem.rightBarButtonItem = barItem;
    }
}

- (void)navigationBarBackgroundWithImage:(UIImage *)backgroundImage
{
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
    bar.translucent = NO;
    self.edgesForExtendedLayout = NO;
}

- (void)navigationBarBackgroundWithColor:(UIColor *)backgroundColor
{
    UINavigationBar *bar = self.navigationController.navigationBar;
    [self.navigationController.navigationBar setBackgroundColor:backgroundColor];
    bar.translucent = NO;
    self.edgesForExtendedLayout = NO;
}

- (void)navigationBarTitle:(NSString *)title attribute:(NSDictionary *)attribute
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.attributedText = [[NSAttributedString alloc] initWithString:title attributes:attribute];
    titleLabel.frame = [titleLabel textRectForBounds:CGRectMake(0, 0, CGFLOAT_MAX, 44.0f) limitedToNumberOfLines:1];
    self.navigationItem.titleView = titleLabel;
}

@end
